<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\Good;
use app\admin\model\Order_good;
use app\BaseController;
use PHPMailer\PHPMailer\PHPMailer;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use think\facade\Session;
use think\facade\View;
use think\Request;

class order extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
       return  View::fetch();
    }

    public function orders(Request $request){
        $params = $request->param();
        $limit = $params['limit'];
       $order_no = input('order_no');
       $type = input('type');
       $status = input('status');
        $where = array();
        if(!empty($order_no)){
            $where['order_no'] = $order_no;
        }
        if(!empty($type)){
            $where['type'] = $type ;
        }
        if(!empty($status)){
            $where['status'] = $status;
        }
       if (!empty($where)){
             $data = \app\admin\model\Order::where($where)->paginate($limit)->toArray();
       }else{
           $data = \app\admin\model\Order::paginate($limit)->toArray();
       }
        return json(['count'=>$data['total'],'code'=>0,'data'=>$data['data']]);
    }

    public function export(){
        $data= \app\admin\model\Order::select()->toArray();
        $spreadsheet= new Spreadsheet();
            // 获取活动单元格
            $sheet = $spreadsheet->getActiveSheet();

            // 获取单元格
            $cellA = $sheet->getCell('A1');
            // 设置单元格的值
            $cellA->setValue('入库单号');
            // 设置 A 列 列宽
            $sheet->getColumnDimension('A')->setWidth(10);
            // 设置第一行 行高
            $sheet->getRowDimension(1)->setRowHeight(20);

            $cellB = $sheet->getCell('B1');
            $cellB->setValue('入库门店');
            $sheet->getColumnDimension('B')->setWidth(20);

            $cellC = $sheet->getCell('C1');
            $cellC->setValue('入库类型');
            $sheet->getColumnDimension('C')->setWidth(10);

            $cellD = $sheet->getCell('D1');
            $cellD->setValue('入库数量');
            $sheet->getColumnDimension('D')->setWidth(20);

            $cellE = $sheet->getCell('E1');
            $cellE->setValue('经办人员');
            $sheet->getColumnDimension('E')->setWidth(40);

            $cellE = $sheet->getCell('F1');
            $cellE->setValue('制单人员');
            $sheet->getColumnDimension('F')->setWidth(40);

            $cellF = $sheet->getCell('G1');
            $cellF->setValue('制单日期');
            $sheet->getColumnDimension('G')->setWidth(30);

            $cellG = $sheet->getCell('H1');
            $cellG->setValue('审核状态');
            $sheet->getColumnDimension('H')->setWidth(30);




            // 设置样式 标题
            $styleArray = [
                'alignment' => [
                    'horizontal' => 'center', //水平居中
                    'vertical' => 'center', //垂直居中
                ],
                'font' => [
                    'name' => '黑体',
                    'bold' => false,
                    'size' => 10
                ]
            ];
            // 设置样式 正文
            $styleArrayBody = [
                'alignment' => [
                    'horizontal' => 'center', //水平居中
                    'vertical' => 'center', //垂直居中
                ],
                'font' => [
                    'name' => '宋体',
                    'bold' => false,
                    'size' => 10
                ]
            ];
            // 应用样式
            $sheet->getStyle('A1')->applyFromArray($styleArray);
            $sheet->getStyle('B1')->applyFromArray($styleArray);
            $sheet->getStyle('C1')->applyFromArray($styleArray);
            $sheet->getStyle('D1')->applyFromArray($styleArray);
            $sheet->getStyle('E1')->applyFromArray($styleArray);
            $sheet->getStyle('F1')->applyFromArray($styleArray);
            $sheet->getStyle('G1')->applyFromArray($styleArray);
            $sheet->getStyle('H1')->applyFromArray($styleArray);

            // 从 A2 开始填充数据
            foreach ($data as $k => $v) {
                $n = $k + 2;
                // 获取单元格
                $cellA = $sheet->getCell('A' . $n);
                // 设置单元格的值
                $cellA->setValue($v['order_no']);

                $cellB = $sheet->getCell('B' . $n);
                $cellB->setValue($v['shop']);

                $cellC = $sheet->getCell('C' . $n);
                $cellC->setValue($v['type']);

                $cellD = $sheet->getCell('D' . $n);
                $cellD->setValue($v['count']);

                $cellD = $sheet->getCell('E' . $n);
                $cellD->setValue($v['do_people']);

                $cellD = $sheet->getCell('F' . $n);
                $cellD->setValue($v['make_people']);

                $cellE = $sheet->getCell('G' . $n);
                $cellE->setValue($v['make_time']);

                $cellF = $sheet->getCell('H' . $n);
                $cellF->setValue($v['status']);

            }

            $file_name = '入库单.xls';
            // 实例化导出类
            header('Content-Type:application/vnd.ms-excel');
            header('Content-Disposition:attachment;filename=' . $file_name);
            header('Cache-Control:max-age=0');

            $writer = IOFactory::createWriter($spreadsheet, 'Xls');
            $writer->save('php://output');
        }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
       $data = Good::select()->toArray();
        View::assign('data',$data);
        return  View::fetch('add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = $request->param();
        $ids= explode(",",$params['ids']);
        unset($params['ids']);
        unset($params['/admin/Order/save_html']);
        unset($params['ckb-jobid']);
        $this->validate($params,[
            'order_no'=>'require',
            'shop'=>'require',
            'status'=>'require',
            'type'=>'require'
        ]);
        $info = Session::get('user');
        $email = $info['email'];
       $params['make_people'] = $info['nickname'];
       $params['make_time'] = date('Y-m-d',time());
       $model = \app\admin\model\Order::create($params)->toArray();
       foreach ($ids as $v){
           $arr['order_id'] = $model['id'];
           $arr['goods_id'] = $v;
           Order_good::create($arr);
       }
       $this->email($email);


    }

    public function email($email){
        $mail = new PHPMailer(true);
        $mail->CharSet  = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->isSMTP();
        $mail->SMTPDebug = 0;
        //Whether to use SMTP authentication
        $mail->SMTPAuth = true;
        //调试输出格式
        $mail->Debugoutput = 'html';
        //smtp服务器
        $mail->Host = 'SMTP.qq.com';
        //端口 - likely to be 25, 465 or 587
        $mail->Port = '465';
        $mail->SMTPSecure ='ssl';// 使用安全协议 tls,ssl
        //用户名
        $mail->Username = $email;
        //密码
        $mail->Password = 'kbdstabvwhigbbdg';
        //Set who the message is to be sent from
        $mail->setFrom($email,'入库通知');
        //回复地址
        //$mail->addReplyTo('replyto@example.com', 'First Last');
        //接收邮件方
        $mail->addAddress($email, '您好！');  //要发送的地址和设置地址的昵称
        $mail->isHTML(true);// send as HTML
        //标题
        $mail->Subject = '入库须知';
        //邮箱正文
        $mail->Body = '商品入库单已经生成';
        //添加附件
        if (!empty($addAttachment)){
            $mail->addAttachment($addAttachment);
        }
        try {
            $mail->send();
            Log::info('发送成功');
        }catch (\Exception $e){
            Log::info('发送失败');
        }

    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        return  View::fetch();
    }

    public function find(Request $request){
        $order_no = input('order_no');
        $data = \app\admin\model\Order::where('order_no',$order_no)->select()->toArray();
        dd($data);
        return json(['code'=>0,'data'=>$data,'goods'=>$good]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
